package com.unjuanable.safetymonitor.application.controlmng;

import com.unjuanable.safetymonitor.domain.controlmng.ControlBuilder;
import com.unjuanable.safetymonitor.domain.controlmng.ControlBuilderFactory;
import com.unjuanable.safetymonitor.domain.controlmng.ControlRepository;
import com.unjuanable.safetymonitor.domain.controlmng.ControlTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description
 */
@Service
public class ControlService {

    private final ControlBuilderFactory controlBuilderFactory;
    private final ControlRepository controlRepository;

    @Autowired
    public ControlService(
            ControlBuilderFactory controlBuilderFactory,
            ControlRepository controlRepository
    ) {
        this.controlBuilderFactory = controlBuilderFactory;
        this.controlRepository = controlRepository;
    }

    public ControlDto addControl(ControlDto request, Long userId) {
        ControlBuilder controlBuilder = controlBuilderFactory.create();
        ControlTask controlTask = controlBuilder.userId(userId)
                .name(request.getName())
                .target(request.getTarget())
                .startAt(request.getStartAt())
                .endAt(request.getEndAt())
                .createdBy(userId)
                .build();

        controlTask = controlRepository.save(controlTask);
        return buildControlDto(controlTask);
    }

    private ControlDto buildControlDto(ControlTask controlTask) {
        return new ControlDto();
    }
}
